package com.cqut.forum.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.Article;
import com.cqut.forum.entity.ArticleGroup;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.ActiveService;
import com.cqut.forum.service.ArticleGroupService;
import com.cqut.forum.service.GardenService;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.util.PackTo;

/**
 * 花园气泡action
 * @author yangxun
 */
@Controller
public class GardenBallAction {
	@Resource
	private GardenService gardenService;
	@Resource
	private ModuleService moduleService;
	@Resource
	private ActiveService activeService;
	@Resource
	private ArticleGroupService articleGroupService;
	
	@RequestMapping("/garden/ball_info")
	@ResponseBody
	public Map<String, Object> ballInfo(String articleId){
		Map<String, Object> result ;
		if(articleId == null){
			result = new HashMap<String, Object>();
			result.put("result", "信息加载失败,请稍后再试");
			return result;
		}
		result = gardenService.getBallInfo(Integer.parseInt(articleId));
		
		return result;
	}
	
	@SuppressWarnings("deprecation")
	@RequestMapping("/garden/create_ball")
	@ResponseBody
	public Map<String, Object> createBall(HttpServletRequest request,String[] ballInfo){
		User user = (User) request.getSession().getAttribute("user");
		Article article = PackTo.packArticle(user.getUserId(), ballInfo[0]);
		ArticleGroup ag = articleGroupService.getArticleGroup(ballInfo[1]);
		article.setModuleId(moduleService.getModuleIndex("花园"));
		article.setArticleGroup(ag);
		article.setIsReview("all".equals(ballInfo[2])?true:false);
		article = activeService.addActive(article);
		
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("articleGroupId", ag.getArticleGroupId());
		result.put("articleId", article.getArticleId());
		result.put("userPhoto", user.getUserPhoto());
		result.put("userName", user.getUserName());
		result.put("visitTimes", article.getVisitTimes());
		result.put("replyTimes", article.getReviewTimes());
		result.put("kindSrc", ag.getArticlePassword());
		result.put("createTime", article.getPublishTime().toLocaleString());
		
		return result;
	}
	
	@RequestMapping("/garden/garden_init")
	@ResponseBody
	public Map<String, Object> gardenInit(HttpServletRequest request,int index,int size){
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("balls",gardenService.getLastBalls(index,size));
		result.put("ss", request.getSession().getServletContext().getAttribute("ss"));
		
		return result;
	}

	@RequestMapping("/garden/load_garden")
	@ResponseBody
	public List<Map<String, Object>> loadByCase(HttpServletRequest request,String publishTime,String ballKind,
			String userRelation,int index,int size){
		User user = (User)request.getSession().getAttribute("user");
		List<Map<String, Object>> balls = gardenService.loadByCase(user.getUserId(),publishTime,ballKind,
				userRelation,index,size);
		return balls;
	}
	/***************get and set method******************/
	public GardenService getGardenService() {
		return gardenService;
	}
	public void setGardenService(GardenService gardenService) {
		this.gardenService = gardenService;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public ActiveService getActiveService() {
		return activeService;
	}

	public void setActiveService(ActiveService activeService) {
		this.activeService = activeService;
	}
}
